package com.gxar.quick.ar.server.base.model.protocol;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.common.utils.StringSpliceUtils;
import com.gxar.quick.ar.server.base.common.enums.protocol.ArSizeUnitEnum;
import com.gxar.quick.ar.server.base.common.utils.NumberUtils;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import lombok.Data;

import java.util.Objects;

/**
 * AR物体尺寸信息
 *
 * @author zxh
 * @version 1.0
 * @created 2024/01/02.
 */
@Data
public class ArSizeModel {

    /**
     * 长度单位
     */
    private ArSizeUnitEnum unit;

    /**
     * 宽 / X轴方向长度
     */
//    @JsonSerialize(using = PlainDoubleSerialize.class)
    private Double width;

    /**
     * 高 / Y轴方向长度
     */
//    @JsonSerialize(using = PlainDoubleSerialize.class)
    private Double height;

    /**
     * 深 / Z轴方向长度
     */
//    @JsonSerialize(using = PlainDoubleSerialize.class)
    private Double depth;

    /**
     * 宽高比
     */
    private Double widthDivideHeight;

    public ArSizeModel() {

    }

    public ArSizeModel(ArSizeUnitEnum unit, Double width, Double height, Double depth) {
        this.unit = unit;
        this.width = width;
        this.height = height;
        this.depth = depth;
    }

    public ArSizeModel(Double width, Double height, Double depth) {
        this.width = width;
        this.height = height;
        this.depth = depth;
    }

    public ArSizeModel(Double width, Double height) {
        this.width = width;
        this.height = height;
        this.depth = 0d;
    }

    /**
     * 更新宽高比
     */
    public void updateWidthDivideHeight() {
        if (this.width <= 0 || this.height <= 0) {
            throw new APIRuntimeException(ResultCodeEnum.CONVERT_PROTOCOL_PARAM_ERROR);
        }

        this.widthDivideHeight = this.width / this.height;
    }

    @Override
    public String toString() {
        return StringSpliceUtils.splice("{} {} {} {}",
                NumberUtils.formatDouble(width),
                NumberUtils.formatDouble(height),
                NumberUtils.formatDouble(depth),
                unit);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ArSizeModel that = (ArSizeModel) o;
        return Objects.equals(width, that.width) && Objects.equals(height, that.height) && Objects.equals(depth, that.depth);
    }

    @Override
    public int hashCode() {
        return Objects.hash(width, height, depth);
    }

    public static ArSizeModel create(ArSizeModel input) {
        ArSizeModel model = new ArSizeModel(input.unit, input.width, input.height, input.depth);
        model.updateWidthDivideHeight();
        return model;
    }
}
